import { defineStore } from 'pinia'
import request from '@/utils/request'
import type { ApiResponse } from '@/types/api'

// 定义客户接口
interface Customer {
  id: number
  name: string
  address: string
  tax_num: string
  tax_bank: string
  tax_banksn: string
  tax_mobile: string
  tax_address: string
  contact_name?: string
  contact_mobile?: string
  belong_uid: number
  belong_did: number
  is_lock: number
  create_time: string
  update_time: string
  delete_time: number
  // 扩展字段
  belong_name?: string
  belong_department?: string

  grade?: string
  source?: string
  customer_status_name?: string

  customer_type_name?: string
  customer_region?: string
  follow_time?: string
  next_time?: string
  contacter?: string
  mobile?: string
  email?: string
  contract_num?: number
}

// 定义客户联系人接口
interface CustomerContact {
  id: number
  cid: number
  name: string
  mobile: string
  sex: number
  qq: string
  wechat: string
  email: string
  is_default: number
  create_time: string
  admin_id: number
}

// 定义客户跟进记录接口
interface CustomerTrace {
  id: number
  cid: number
  content: string
  file_ids: string
  create_time: string
  admin_id: number
}

// 定义销售机会接口
interface CustomerChance {
  id: number
  cid: number
  name: string
  amount: number
  grade_id: number
  source_id: number
  status: number
  remark: string
  create_time: string
  update_time: string
  admin_id: number
}

// 定义客户日志接口
interface CustomerLog {
  id: number
  field: string
  action: string
  type: number
  customer_id: number
  admin_id: number
  old_content: string
  new_content: string
  create_time: number
}

interface CustomerState {
  customers: Customer[]
  customerTotal: number
  contacts: CustomerContact[]
  contactTotal: number
  traces: CustomerTrace[]
  traceTotal: number
  chances: CustomerChance[]
  chanceTotal: number
  logs: CustomerLog[]
  logTotal: number
}

// 客户相关API
const customerApi = {
  // 获取客户列表
  getCustomerList: (params: any): Promise<ApiResponse<Customer[]>> => {
    return request({
      url: '/api/customer/customer/list',
      method: 'get',
      params,
    })
  },

  // 分配客户
  distributeCustomer: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/customer/customer/assign',
      method: 'post',
      data,
    })
  },

  // 锁定/解锁客户
  lockCustomer: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/customer/customer/lock',
      method: 'post',
      data,
    })
  },

  // 删除客户
  deleteCustomer: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/customer/customer/del',
      method: 'delete',
      data,
    })
  },

  // 获取客户跟进记录
  getTraceList: (params: any): Promise<ApiResponse<CustomerTrace[]>> => {
    return request({
      url: '/api/customer/trace/list',
      method: 'get',
      params,
    })
  },

  // 获取销售机会列表
  getChanceList: (params: any): Promise<ApiResponse<CustomerChance[]>> => {
    return request({
      url: '/api/customer/chance/list',
      method: 'get',
      params,
    })
  },

  // 获取联系人数据
  getContactList: (params: any): Promise<ApiResponse<CustomerContact[]>> => {
    return request({
      url: '/api/customer/contact/list',
      method: 'get',
      params,
    })
  },

  // 设置默认联系人
  setContact: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/customer/contact/set_default',
      method: 'post',
      data,
    })
  },

  // 添加附件
  addFile: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/customer/file/add',
      method: 'post',
      data,
    })
  },

  // 删除附件
  deleteFile: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/customer/file/del',
      method: 'delete',
      data,
    })
  },

  // 获取操作日志列表
  getLogList: (params: any): Promise<ApiResponse<CustomerLog[]>> => {
    return request({
      url: '/api/customer/log/list',
      method: 'get',
      params,
    })
  },
}

export const useCustomerStore = defineStore('customer', {
  state: (): CustomerState => ({
    customers: [],
    customerTotal: 0,
    contacts: [],
    contactTotal: 0,
    traces: [],
    traceTotal: 0,
    chances: [],
    chanceTotal: 0,
    logs: [],
    logTotal: 0,
  }),

  actions: {
    // 获取客户列表
    async getCustomerListAction(params: any) {
      try {
        const res: ApiResponse<Customer[]> = await customerApi.getCustomerList(params)
        if (res.code === 0) {
          this.customers = res.data || []
          this.customerTotal = res.count || 0
        }
        return res
      } catch (error) {
        throw error
      }
    },

    // 分配客户
    async distributeCustomerAction(data: any) {
      try {
        const res = await customerApi.distributeCustomer(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 锁定/解锁客户
    async lockCustomerAction(data: any) {
      try {
        const res = await customerApi.lockCustomer(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 删除客户
    async deleteCustomerAction(data: any) {
      try {
        const res = await customerApi.deleteCustomer(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 获取客户跟进记录
    async getTraceListAction(params: any) {
      try {
        const res: ApiResponse<CustomerTrace[]> = await customerApi.getTraceList(params)
        if (res.code === 0) {
          this.traces = res.data || []
          this.traceTotal = res.count || 0
        }
        return res
      } catch (error) {
        throw error
      }
    },

    // 获取销售机会列表
    async getChanceListAction(params: any): Promise<ApiResponse<CustomerChance[]>> {
      try {
        const res = await customerApi.getChanceList(params)
        if (res.code === 0) {
          this.chances = res.data || []
          this.chanceTotal = res.count || 0
        }
        return res
      } catch (error) {
        throw error
      }
    },

    // 获取联系人数据
    async getContactListAction(params: any): Promise<ApiResponse<CustomerContact[]>> {
      try {
        const res = await customerApi.getContactList(params)
        if (res.code === 0) {
          this.contacts = res.data || []
          this.contactTotal = res.count || 0
        }
        return res
      } catch (error) {
        throw error
      }
    },

    // 设置默认联系人
    async setContactAction(data: any): Promise<ApiResponse<any>> {
      try {
        const res = await customerApi.setContact(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 添加附件
    async addFileAction(data: any): Promise<ApiResponse<any>> {
      try {
        const res = await customerApi.addFile(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 删除附件
    async deleteFileAction(data: any): Promise<ApiResponse<any>> {
      try {
        const res = await customerApi.deleteFile(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 获取操作日志列表
    async getLogListAction(params: any): Promise<ApiResponse<CustomerLog[]>> {
      try {
        const res = await customerApi.getLogList(params)
        if (res.code === 0) {
          this.logs = res.data || []
          this.logTotal = res.count || 0
        }
        return res
      } catch (error) {
        throw error
      }
    },
  },
})
